package com.lej.theme.dialog

import android.content.Context
import android.util.TypedValue
import android.widget.FrameLayout
import androidx.annotation.DrawableRes
import androidx.core.view.isGone
import com.lej.theme.R
import com.lej.theme.databinding.ThemeHorDialogBinding
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.qmuiteam.qmui.kotlin.onClick

/**
 *  author: cjq
 *  date  : 2022/7/4 0004
 *  desc  :
 */
class HorAlertPopDialog(
    context: Context, var title: CharSequence?,
    var content: CharSequence?,
    var confirmText: CharSequence = "确认",
    var cancelText: CharSequence = "再等等",
    val hideCancel: Boolean = false,
    var showClose: Boolean = false,//显示关闭按钮
    var tipsLogo: Int = 0,//弹窗logo
    var confirmClose:Boolean = true,
    var autoSizeLayout:Boolean = true,
    val cancel: () -> Unit = {},
    val onConfirm: () -> Unit = {}
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int = R.layout.theme_hor_dialog

    override fun initPopupContent() {
        super.initPopupContent()
        val binding =
            ThemeHorDialogBinding.bind(centerPopupContainer.findViewById(R.id.cl_dialog))
        binding.tvTitle.text = title ?: ""
        binding.tvTitle.isGone = title.isNullOrEmpty()
        binding.tvContent.text = content?:""
        binding.tvPositive.text = confirmText
        binding.tvNegative.text = cancelText

        if(!autoSizeLayout){
            binding.tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,16f)
            binding.tvContent.setTextSize(TypedValue.COMPLEX_UNIT_SP,15f)
            binding.tvNegative.setTextSize(TypedValue.COMPLEX_UNIT_SP,14f)
            binding.tvPositive.setTextSize(TypedValue.COMPLEX_UNIT_SP,14f)
        }
        binding.tvNegative.onClick {
            cancel()
            dismiss()
        }
        binding.tvPositive.onClick {
            onConfirm()
            if(confirmClose) dismiss()
        }
        if (!hideCancel) return
        binding.clDialog.onClick { }
        val parent: FrameLayout = binding.clDialog.parent as FrameLayout
        parent.onClick { dismiss() }
    }

    companion object {
        fun show(
            context: Context,
            title: CharSequence? = "",
            content: CharSequence? = "",
            confirmText: CharSequence = "确认",
            cancelText: CharSequence = "再等等",
            showClose: Boolean = false,//显示关闭按钮
            @DrawableRes tipsLogo: Int = 0,//弹窗logo
            hasClose: Boolean = true,//是否可以点击空白关闭弹窗
            hideCancel: Boolean = true,
            confirmClose:Boolean = true,//确定按钮点击后是否关闭
            autoSizeLayout:Boolean = true,//是否是适配框架布局
            cancel: () -> Unit = {},
            onConfirm: () -> Unit = {}
        ): BasePopupView {
            return XPopup.Builder(context)
                .hasStatusBar(false)
                .dismissOnTouchOutside(hasClose)
                .dismissOnBackPressed(hasClose)
                .asCustom(
                    HorAlertPopDialog(
                        context,
                        title = title,
                        content = content,
                        confirmText = confirmText,
                        cancelText = cancelText,
                        hideCancel = hideCancel,
                        showClose = showClose,
                        tipsLogo = tipsLogo,
                        autoSizeLayout= autoSizeLayout,
                        confirmClose = confirmClose,
                        cancel = cancel,
                        onConfirm = onConfirm
                    )
                )
                .show()
        }
    }
}